#include "searchdevicewin.h"

#include <QDebug>
#include <QScreen>
#include <QApplication>
#include "windowmanage.hpp"

SearchDeviceWin::SearchDeviceWin(QWidget *parent) : QWidget(parent)
{
    initUI();
    initSearchButton();
}

SearchDeviceWin::~SearchDeviceWin()
{
    if (m_loadAnimation != nullptr) {
        m_loadAnimation->deleteLater();
        m_loadAnimation = nullptr;
    }
}

void SearchDeviceWin::initUI()
{
    m_stackedWidget = new QStackedWidget(this);

    QHBoxLayout *listWidgetHLayout = new QHBoxLayout();
    listWidgetHLayout->setContentsMargins(8, 8, 0, 8);
    listWidgetHLayout->addWidget(m_stackedWidget);

    m_listWidget = new QListWidget(this);
    //设置显示图标模式
    m_listWidget->setViewMode(QListView::IconMode);
    //自适应布局
    m_listWidget->setResizeMode(QListView::Adjust);
    //列表中的图标不可拖动
    m_listWidget->setMovement(QListView::Static);
    //去边框
    m_listWidget->setFrameShape(QListWidget::NoFrame);

    //    m_listWidgetItem = new QListWidgetItem(m_listWidget);
    //    m_continueSearchDeviceItem = new AddDeviceItem();
    //    m_listWidget->setItemWidget(m_listWidgetItem, m_continueSearchDeviceItem);
    //    m_listWidgetItem->setSizeHint(QSize(234, 80));
    //    m_continueSearchDeviceItem->setFocusPolicy(Qt::NoFocus);

    m_stackedWidget->addWidget(m_listWidget);

    m_loadAnimation = new LoadAnimation;
    m_loadAnimation->setHighLight();
    QWidget *loadWid = new QWidget(this);
    QLabel *loadLab = new QLabel(this);
    loadLab->setText(tr("Searching for nearby available devices..."));

    QHBoxLayout *loadHLayout = new QHBoxLayout();
    loadHLayout->setContentsMargins(0, 0, 0, 0);
    loadHLayout->addStretch();
    loadHLayout->addWidget(loadLab);
    loadHLayout->addStretch();

    QVBoxLayout *loadVLayout = new QVBoxLayout();
    loadVLayout->setContentsMargins(0, 0, 0, 0);
    loadVLayout->setSpacing(0);
    loadVLayout->addStretch();
    loadVLayout->addWidget(m_loadAnimation);
    loadVLayout->addLayout(loadHLayout);
    loadVLayout->addStretch();
    loadWid->setLayout(loadVLayout);
    m_stackedWidget->addWidget(loadWid);
    m_stackedWidget->setCurrentIndex(1);

    m_listWidget->setSpacing(8);

    QVBoxLayout *mainVLayout = new QVBoxLayout();
    mainVLayout->setContentsMargins(0, 0, 0, 0);
    mainVLayout->addWidget(m_stackedWidget);
    this->setLayout(mainVLayout);
}

void SearchDeviceWin::showLoad()
{
    isSearchInitFinish = false;
    m_searchBtn->hide();
    m_stackedWidget->setCurrentIndex(1);
}

void SearchDeviceWin::setSearchBtnState(bool isShow)
{
    isShowSearchBtn = isShow;
    if (isShowSearchBtn && isSearchInitFinish) {
        m_searchBtn->show();
    } else {
        m_searchBtn->hide();
    }
}

void SearchDeviceWin::setItemInfo(const QMap<QString, KMobileCommon::DiscoveryDeviceInfo> &map)
{
    qInfo() << "device count" << map.size();
    m_listWidget->clear();
    QMap<QString, KMobileCommon::DiscoveryDeviceInfo>::const_iterator it = map.begin();

    while (it != map.end()) {
        QListWidgetItem *item = new QListWidgetItem(m_listWidget);
        SearchDeviceItem *searchDeviceItem = new SearchDeviceItem();
        m_listWidget->setItemWidget(item, searchDeviceItem);
        item->setSizeHint(QSize(234, 80));
        searchDeviceItem->setFocusPolicy(Qt::NoFocus);
        if (it.value().deviceType == KMobileCommon::DeviceType::PC) {
            searchDeviceItem->setItemIcon(":/devicetype/pc.svg");
        } else if (it.value().deviceType == KMobileCommon::DeviceType::Android) {
            searchDeviceItem->setItemIcon(":/devicetype/tv.svg");
        } else if (it.value().deviceType == KMobileCommon::DeviceType::Phone) {
            searchDeviceItem->setItemIcon(":/devicetype/phone.svg");
        }
        //设置IP
        searchDeviceItem->setItemIP(it.key());
        //设备名
        searchDeviceItem->setItemName(it.value().deviceName);
        //设备信息
        searchDeviceItem->setItemDeviceInfo(it.value());
        connect(searchDeviceItem, &SearchDeviceItem::clicked, this, [=]() {
            QString itemIP = searchDeviceItem->getItemIp();
            KMobileCommon::DiscoveryDeviceInfo itemDeviceInfo = searchDeviceItem->getItemDeviceInfo();
            Q_EMIT sigItemInfo(itemDeviceInfo.ip);
        });
        it++;
    }
    m_stackedWidget->setCurrentIndex(0);
    isSearchInitFinish = true;
    if (isShowSearchBtn && isSearchInitFinish) {
        m_searchBtn->show();
    }
}

void SearchDeviceWin::initSearchButton()
{
    m_searchBtn = new LevitationButton(this);
    m_searchBtn->setIcon(":/connectinterface/searchrefresh.svg");
    m_searchBtn->setText(tr("Search"));
    m_searchBtn->hide();
    connect(m_searchBtn, &LevitationButton::clicked, this, [=]() {
        qInfo() << "continue search clicked";
        Q_EMIT sigContinueSearch();
    });
}

void SearchDeviceWin::moveSearchBtn()
{
    m_searchBtn->move(
        QPoint(this->width() / 2 - m_searchBtn->width() / 2, this->height() - 16 - m_searchBtn->height()));
}

void SearchDeviceWin::setTheme(PublicAttributes::Theme theme)
{
    switch (theme) {
    case PublicAttributes::Theme::Light: {
        m_listWidget->setStyleSheet("background: #F6F6F6;");
    } break;
    case PublicAttributes::Theme::Dark: {
        m_listWidget->setStyleSheet("background: #232426;");
    } break;
    }
}